package org.example.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.entity.News;
import org.example.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author guocb
 * @since 2021-03-06
 */
@Controller
@RequestMapping("/news")
public class NewsController {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    INewsService newsService;

    @GetMapping("/list")
    public String list(Model model, String t, String a) {

//        List<News> newsList = new ArrayList<>();
//        if(redisTemplate.hasKey("newsList")){
//            //查缓存
//            String jsonString = redisTemplate.opsForValue().get("newsList");
//            newsList = JSON.parseArray(jsonString,News.class);
//        }else{
//            newsList = newsService.list();
//            //添加到缓存  JSON类型的字符串
//            redisTemplate.opsForValue().set("newsList", JSON.toJSONString(newsList));
//        }
        model.addAttribute("newsList", newsService.list(t,a));
        return "newslist";
    }
    //增删改清除缓存
}
